# Load packages
librarian::shelf(
    dplyr, readr, skimr, tidyr,
    quiet = TRUE
)

# Overwrite select function
select <- dplyr::select

# Import data
## Study 2a
data <- read_csv(
    "Data/Meta/braleyEtAl_2023/study2_data.csv",
    show_col_types = FALSE
)

# Remove junk rows
data <- data[-c(1, 2), ]

# Remove people who didn't consent to the survey
data <- data %>%
    subset(consent == "Yes")

# Remove people who failed attention checks
data <- data %>%
    subset(attention == "I understand,I don't understand" | is.na(attention))

data <- data %>%
    subset(robot == "I am a robot" | is.na(robot))

# Remove people with no party
data <- data %>%
    subset(!is.na(party))

# Remove people who were kicked out for being Independents
data <- data %>%
    subset(c_ind_leaning != "Neither" | is.na(c_ind_leaning))

data <- data %>%
    subset(t_ind_leaning != "Neither" | is.na(t_ind_leaning))

# Rename variables
data <- data %>%
    rename(
        condition = t,
        feel_dem = feel_1,
        feel_rep = feel_2
    )

# Drop missing values
data[data == "-99"] <- NA_character_

#################
### CONDITION ###
#################

# Recode condition variable
data <- data %>%
    mutate(condition = case_when(
        condition == "1" ~ "treatment",
        condition == "0" ~ "control"
    ))

##############
### WARMTH ###
##############

data <- data %>%
    mutate(
        feel_dem = feel_dem %>% as.numeric(),
        feel_rep = feel_rep %>% as.numeric(),
        feel_out = case_when(
            party == "Democrat" ~ feel_rep,
            party == "Republican" ~ feel_dem
        ),
        feel_difference = case_when(
            party == "Democrat" ~ feel_dem - feel_rep,
            party == "Republican" ~ feel_rep - feel_dem
        )
    )

######################
### DEHUMANIZATION ###
######################

# Consolidate answers
data$dehum_t <- ifelse(is.na(data$t_rep_dehum_1), data$t_dem_dehum_1, data$t_rep_dehum_1)
data$dehum_c <- ifelse(is.na(data$c_rep_dehum_1), data$c_dem_dehum_1, data$c_rep_dehum_1)
data$dehumanization <- ifelse(is.na(data$dehum_t), data$dehum_c, data$dehum_t)

# Make numeric and flip
data$dehumanization <- 100 - data$dehumanization %>% as.numeric()

##################
### SUBVERSION ###
##################

# Consolidate answers
data$rallies_t <- ifelse(is.na(data$t_rep_rallies), data$t_dem_rallies, data$t_rep_rallies)
data$rallies_c <- ifelse(is.na(data$c_rep_rallies), data$c_dem_rallies, data$c_rep_rallies)
data$rallies <- ifelse(is.na(data$rallies_t), data$rallies_c, data$rallies_t)

data$judges_t <- ifelse(is.na(data$t_rep_judges), data$t_dem_judges, data$t_rep_judges)
data$judges_c <- ifelse(is.na(data$c_rep_judges), data$c_dem_judges, data$c_rep_judges)
data$judges <- ifelse(is.na(data$judges_t), data$judges_c, data$judges_t)

data$journ_t <- ifelse(is.na(data$t_rep_journ), data$t_dem_journ, data$t_rep_journ)
data$journ_c <- ifelse(is.na(data$c_rep_journ), data$c_dem_journ, data$c_rep_journ)
data$journ <- ifelse(is.na(data$journ_t), data$journ_c, data$journ_t)

data$voting_t <- ifelse(is.na(data$t_rep_voting), data$t_dem_voting, data$t_rep_voting)
data$voting_c <- ifelse(is.na(data$c_rep_voting), data$c_dem_voting, data$c_rep_voting)
data$voting <- ifelse(is.na(data$voting_t), data$voting_c, data$voting_t)

data$elected_t <- ifelse(is.na(data$t_rep_elected), data$t_dem_elected, data$t_rep_elected)
data$elected_c <- ifelse(is.na(data$c_rep_elected), data$c_dem_elected, data$c_rep_elected)
data$elected <- ifelse(is.na(data$elected_t), data$elected_c, data$elected_t)

data$violence_t <- ifelse(is.na(data$t_rep_violence), data$t_dem_violence, data$t_rep_violence)
data$violence_c <- ifelse(is.na(data$c_rep_violence), data$c_dem_violence, data$c_rep_violence)
data$violence <- ifelse(is.na(data$violence_t), data$violence_c, data$violence_t)

data$const_t <- ifelse(is.na(data$t_rep_const), data$t_dem_const, data$t_rep_const)
data$const_c <- ifelse(is.na(data$c_rep_const), data$c_dem_const, data$c_rep_const)
data$const <- ifelse(is.na(data$const_t), data$const_c, data$const_t)

# Recode Likert responses
data <- data %>%
    mutate(
        across(
            c(rallies, judges, journ, voting, elected, violence, const),
            ~ case_when(
                grepl("Never", .) ~ 0,
                grepl("Probably Not", .) ~ 1,
                grepl("Probably", .) ~ 2,
                grepl("Definitely", .) ~ 3
            )
        )
    )

# Generate index
data$subversion <- data %>%
    select(rallies, judges, journ, voting, elected, violence, const) %>%
    rowMeans(na.rm = TRUE)

# Get ranges
max(data$subversion, na.rm = TRUE) - min(data$subversion, na.rm = TRUE)
max(data$feel_out, na.rm = TRUE) - min(data$feel_out, na.rm = TRUE)
max(data$feel_difference, na.rm = TRUE) - min(data$feel_difference, na.rm = TRUE)
max(data$dehumanization, na.rm = TRUE) - min(data$dehumanization, na.rm = TRUE)

##############
### MODELS ###
##############

# Democratic Subversion
lm(subversion ~ condition, data = data) %>% summary()

# Out-Party Warmth
lm(feel_out ~ condition, data = data) %>% summary()

# Affective Polarization
lm(feel_difference ~ condition, data = data) %>% summary()

# Dehumanization
lm(dehumanization ~ condition, data = data) %>% summary()
